package com.teaching.Dao;

import com.teaching.Enity.Score;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ScoreDaoAchieve implements ScoreDao {

    private SqlSession sqlSession;
    private String resource = "mybatis.xml";

    @Override//将成绩存入数据库
    public void AddScore(Score score) throws IOException{
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        sqlSession.insert("addScore",score);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override//从数据库获取全部成绩
    public List<Score> AllScore() throws  IOException{
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        List<Score> scoreList = sqlSession.selectList("allScore");
        sqlSession.commit();
        sqlSession.close();
        return scoreList;
    }
}
